export function getDateStar() {
  let date = "";
  if (new Date().getDate() > 4) {
    if (new Date().getMonth() == 0) {
      date = new Date().getFullYear() - 1 + "-" + 12;
    } else {
      date = new Date().getFullYear() + "-" + new Date().getMonth();
    }
  } else {
    if (new Date().getMonth() == 0) {
      date = new Date().getFullYear() - 1 + "-" + 11;
    } else if (new Date().getMonth() == 1) {
      date = new Date().getFullYear() - 1 + "-" + 12;
    } else {
      date = new Date().getFullYear() + "-" + new Date().getMonth() - 1;
    }
  }
  return date;
}

export function timeFormat(timeDate, fmt) {
  if (!timeDate) {
    return "";
  }
  if (Number(timeDate)) {
    timeDate = Number(timeDate);
  }
  let time = new Date(timeDate);
  //debugger;
  let o = {
    "M+": time.getMonth() + 1, //月份
    "d+": time.getDate(), //日
    "h+": time.getHours(), //小时
    "m+": time.getMinutes(), //分
    "s+": time.getSeconds(), //秒
    "q+": Math.floor((time.getMonth() + 3) / 3), //季度
    S: time.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (time.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (let k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  return fmt;
}

export function getCurrentWeekFirstDay(date) {
  let weekFirstDay = new Date(date - (date.getDay() - 1) * 86400000);
  let firstMonth = Number(weekFirstDay.getMonth()) + 1;

  if (firstMonth < 10) {
    firstMonth = "0" + firstMonth;
  }
  let weekFirstDays = weekFirstDay.getDate();
  if (weekFirstDays < 10) {
    weekFirstDays = "0" + weekFirstDays;
  }
  return weekFirstDay.getFullYear() + "-" + firstMonth + "-" + weekFirstDays;
}

export function getCurrentWeekLastDay(date) {
  let weekFirstDay = new Date(date - (date.getDay() - 1) * 86400000);
  let weekLastDay = new Date((weekFirstDay / 1000 + 6 * 86400) * 1000);
  let lastMonth = Number(weekLastDay.getMonth()) + 1;
  if (lastMonth < 10) {
    lastMonth = "0" + lastMonth;
  }
  let weekLastDays = weekLastDay.getDate();
  if (weekLastDays < 10) {
    weekLastDays = "0" + weekLastDays;
  }
  return weekFirstDay.getFullYear() + "-" + lastMonth + "-" + weekLastDays;
}

export function getCurrentMonthFirstDay() {
  let date = new Date();
  date.setDate(1);
  let month = parseInt(date.getMonth() + 1);
  let day = date.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day < 10) {
    day = "0" + day;
  }
  return date.getFullYear() + "-" + month + "-" + day;
}

export function getCurrentMonthLastDay() {
  let date = new Date();
  let currentMonth = date.getMonth();
  let nextMonth = ++currentMonth;
  let nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
  let oneDay = 1000 * 60 * 60 * 24;
  let lastTime = new Date(nextMonthFirstDay - oneDay);
  let month = parseInt(lastTime.getMonth() + 1);
  let day = lastTime.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day < 10) {
    day = "0" + day;
  }
  return date.getFullYear() + "-" + month + "-" + day;
}

export function getWeek(date) {
  const weekNum = date.getDay();
  switch (weekNum) {
    case 0:
      return "星期日";
    case 1:
      return "星期一";
    case 2:
      return "星期二";
    case 3:
      return "星期三";
    case 4:
      return "星期四";
    case 5:
      return "星期五";
    case 6:
      return "星期六";
  }
}

